<?php 
namespace plugin;
class Admin extends \Plugin {
	
	protected $_admin_auth = 1;

	public function index(){
		$links = \Ebcms::db() -> select('link', '*', [
			'ORDER' => [
				'weight' => 'ASC',
				'id' => 'ASC',
			]
		]);
		$this -> assign('links', $links);
		return \mylib\Response::html($this -> fetch());
	}

	public function add(){
		if (\Ebcms::isPost()) {
			\Ebcms::db() -> insert('link', [
				'title'=>\mylib\Input::post('title'),
				'url'=>\mylib\Input::post('url'),
				'logo'=>\mylib\Input::post('logo'),
				'weight'=>\mylib\Input::post('weight'),
				'remark'=>\mylib\Input::post('remark'),
			]);
			return $this -> success('操作成功！', plugin_url('/link/admin/index'));
		}else{
			return \mylib\Response::html($this -> fetch());
		}
	}

	public function edit(){
		if (\Ebcms::isPost()) {
			\Ebcms::db() -> update('link', [
				'title'=>\mylib\Input::post('title'),
				'url'=>\mylib\Input::post('url'),
				'logo'=>\mylib\Input::post('logo'),
				'weight'=>\mylib\Input::post('weight'),
				'remark'=>\mylib\Input::post('remark'),
			], [
				'id'=>\mylib\Input::post('id')
			]);
			return $this -> success('操作成功！', plugin_url('/link/admin/index'));
		}else{
			$link = \Ebcms::db() -> get('link', '*', ['id'=>\mylib\Input::get('id')]);
			$this -> assign('link', $link);
			return \mylib\Response::html($this -> fetch());
		}
	}

	public function del(){
		if (\Ebcms::isAjax()) {
			\Ebcms::db() -> delete('link', [
				'id'=>\mylib\Input::post('id'),
			]);
			return $this -> success('操作成功！');
		}
	}
}